﻿"use strict";
define(['admin/app', "service/admin/helpService", "angular-kindeditor", "ui.file.manage"], function (app) {
    app.controller('helpController', function ($scope, $stateParams, $location, $uibModal, $state, utils, helpService) {
        $scope.query = {
            page: 1, size: 10, type:1
        };
        $scope.editConfig = {
            autoHeightMode: true,
            items: ['source', 'preview', '|', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', 'image', 'multiimage',]
        };
        $scope.items_query = {
            page: 1, size: 3, article_id: ''
        }
        $.extend($scope.query, $stateParams);
        var methods = {
            textAreaChanged: function (type) {
                if (type == 1) {
                    $scope.textcount = 2000 - $scope.model.description.length;
                    $("#textcount").text($scope.model.description.length + "/" + $scope.textcount);
                } else if (type == 2) {
                    $scope.textcount = 500 - $scope.item.reply_content.length;
                    $("#textcount").text($scope.item.reply_content.length + "/" + $scope.textcount);
                }

                if ($scope.textcount <= 0) {
                    $scope.textcount = 0;
                    $("#textcount").css("color", "red")
                } else {
                    $("#textcount").css("color", "#ccc")
                }


                if ($scope.model.description.length > 2000) {
                    $scope.model.description = $scope.model.description.substr(0, 2000);
                }

                if ($scope.item.reply_content.length > 500) {
                    $scope.item.reply_content = $scope.item.describe.substr(0, 500);
                }
            },
            search: function () {
                helpService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                    $.each($scope.model.rows, function (i, item) {
                        item.description = item.description.replace(/&nbsp;/gi, "");
                    });
                });
            },
            get: function () {
                if ($stateParams.id > 0) {
                    helpService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.items_query.article_id = $stateParams.id;
                    helpService.getdetails($scope.items_query).success(function (response) {
                        $scope.items = response.data;
                        $.each($scope.items.rows, function (i, item) {
                            item.reply_content = item.reply_content.replace(/&nbsp;/gi, "");
                        });

                    });
                }
            },
            deleteitem: function (id) {
                var modal = utils.confirm({ msg: "您确定要删除该回帖吗?", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    helpService.deleteitem(id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            utils.remove($scope.model, model);
                    })
                })
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    helpService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                } else {
                    $scope.breadcrumb = '新增';
                }
            },
            delete: function (id) {
                var modal = utils.confirm({ msg: "您确定要删除该文章吗?", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    helpService.delete(id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            utils.remove($scope.model, model);
                    })
                })
            },
            quality: function (id,is_quality) {
                var modal = utils.confirm({ msg: is_quality?"您确定要显示该文章么吗?":"您确定要隐藏该文章么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    helpService.quality(id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.search();
                    })
                })
            },
            top: function (id,is_top) {
                var modal = utils.confirm({ msg: is_top ? "您确定要推荐该文章吗?" :"您确定要取消推荐该文章吗?", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    helpService.top(id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.search();
                    })
                })
            },
            reply: function () {
                var model = $scope.item;
                model.article_id = $scope.model.id;
                helpService.reply(model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.goBack();
                    else
                        $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            save: function () {
                var model = $scope.model;
                model.type = 1;
                $scope.psdisabled = true;
                helpService.add(model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.goBack();
                    else
                        $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            goBack: function () {
                history.back();
            },
            search_page: function (page, type) {
                if (type == 1) {
                    $scope.query.page = page;
                    methods.search('page', $scope.query.page);
                }
                else if (type == 2) {
                    $scope.items_query.page = page;
                    methods.get();
                }


            }
        }

        angular.extend($scope, methods);
        if ($state.includes('app.help.detail')) {
            methods.edit();
        }
        else {
            methods.search();
        }
    });
});
